﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using BLL.Common;
using System.Web.UI.HtmlControls;

namespace App
{
    public partial class BBS_ThreadList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void rptThreadList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                UserPaperInfo p = e.Item.DataItem as UserPaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_thread") as HtmlAnchor;

                if (p != null && p.UserPaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject + " (" + p.DateCreated.ToShortDateString() + ")";
                    link.HRef = "ThreadInfo.aspx?" + Contexts.ThreadId + "=" + Contexts.GetEncrypt(p.UserPaperId.ToString());

                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptThreadList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void anpOrders_PageChanged(object sender, EventArgs e)
        {
            pageInit();
        }

        private void pageInit()
        {
            //int newsType = Contexts.GetNewsType();
            List<UserPaperInfo> Pages = getPages();
            if (Pages != null)
            {

                this.rptThreadList.DataSource = Pages;
                this.rptThreadList.DataBind();
            }
        }

        private List<UserPaperInfo> getPages()
        {
            List<UserPaperInfo> userPapers = BLL.BUserPaper.getInstance().GetPageByIsBBS(1);


            if (userPapers != null && userPapers.Count > 0)
            {
                this.anpOrders.RecordCount = userPapers.Count;
                List<UserPaperInfo> tempPages = new List<UserPaperInfo>();
                tempPages.AddRange(userPapers);
                int currentNum = 0;

                //分页数据处理
                foreach (UserPaperInfo info in tempPages)
                {
                    currentNum++;
                    if (currentNum < anpOrders.PageSize * (anpOrders.CurrentPageIndex - 1) + 1 || currentNum > anpOrders.PageSize * anpOrders.CurrentPageIndex)
                    {
                        userPapers.Remove(info);
                    }
                }
            }

            return userPapers;
        }
    }
}